<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="井ID" prop="wellId">
        <el-input v-model="formData.wellId" placeholder="请输入井ID" />
      </el-form-item>
      <el-form-item label="井号" prop="wellName">
        <el-input v-model="formData.wellName" placeholder="请输入井号" />
      </el-form-item>
      <el-form-item label="记录标识符" prop="recId">
        <el-input v-model="formData.recId" placeholder="请输入记录标识符" />
      </el-form-item>
      <el-form-item label="序列标识符" prop="seqId">
        <el-input v-model="formData.seqId" placeholder="请输入序列标识符" />
      </el-form-item>
      <el-form-item label="日期（数据中心电脑的日期）" prop="dataDate">
        <el-date-picker
          v-model="formData.dataDate"
          type="date"
          value-format="x"
          placeholder="选择日期（数据中心电脑的日期）"
        />
      </el-form-item>
      <el-form-item label="时间（数据中心电脑的时间）" prop="dataTime">
        <el-date-picker
          v-model="formData.dataTime"
          type="date"
          value-format="x"
          placeholder="选择时间（数据中心电脑的时间）"
        />
      </el-form-item>
      <el-form-item label="日期" prop="date">
        <el-date-picker
          v-model="formData.date"
          type="date"
          value-format="x"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="时间" prop="time">
        <el-date-picker
          v-model="formData.time"
          type="date"
          value-format="x"
          placeholder="选择时间"
        />
      </el-form-item>
      <el-form-item label="活动码" prop="actCode">
        <el-input v-model="formData.actCode" placeholder="请输入活动码" />
      </el-form-item>
      <el-form-item label="测量深度" prop="measDepth">
        <el-input v-model="formData.measDepth" placeholder="请输入测量深度" />
      </el-form-item>
      <el-form-item label="垂直深度" prop="vertDepth">
        <el-input v-model="formData.vertDepth" placeholder="请输入垂直深度" />
      </el-form-item>
      <el-form-item label="钻头深度" prop="bitDepth">
        <el-input v-model="formData.bitDepth" placeholder="请输入钻头深度" />
      </el-form-item>
      <el-form-item label="钻头垂深" prop="bitVDepth">
        <el-input v-model="formData.bitVDepth" placeholder="请输入钻头垂深" />
      </el-form-item>
      <el-form-item label="通过次数" prop="passNum">
        <el-input v-model="formData.passNum" placeholder="请输入通过次数" />
      </el-form-item>
      <el-form-item label="深电阻率深度" prop="r1Depth">
        <el-input v-model="formData.r1Depth" placeholder="请输入深电阻率深度" />
      </el-form-item>
      <el-form-item label="深电阻率垂深" prop="r1VDepth">
        <el-input v-model="formData.r1VDepth" placeholder="请输入深电阻率垂深" />
      </el-form-item>
      <el-form-item label="深电阻率读数" prop="r1Reading">
        <el-input v-model="formData.r1Reading" placeholder="请输入深电阻率读数" />
      </el-form-item>
      <el-form-item label="深电阻率校正读数" prop="r1CReading">
        <el-input v-model="formData.r1CReading" placeholder="请输入深电阻率校正读数" />
      </el-form-item>
      <el-form-item label="浅电阻率深度" prop="r2Depth">
        <el-input v-model="formData.r2Depth" placeholder="请输入浅电阻率深度" />
      </el-form-item>
      <el-form-item label="浅电阻率垂深" prop="r2VDepth">
        <el-input v-model="formData.r2VDepth" placeholder="请输入浅电阻率垂深" />
      </el-form-item>
      <el-form-item label="浅电阻率读数" prop="r2Reading">
        <el-input v-model="formData.r2Reading" placeholder="请输入浅电阻率读数" />
      </el-form-item>
      <el-form-item label="浅电阻率校正读数" prop="r2CReading">
        <el-input v-model="formData.r2CReading" placeholder="请输入浅电阻率校正读数" />
      </el-form-item>
      <el-form-item label="1号伽玛射线深度" prop="g1Depth">
        <el-input v-model="formData.g1Depth" placeholder="请输入1号伽玛射线深度" />
      </el-form-item>
      <el-form-item label="1号伽玛射线垂深" prop="g1VDepth">
        <el-input v-model="formData.g1VDepth" placeholder="请输入1号伽玛射线垂深" />
      </el-form-item>
      <el-form-item label="1号伽玛射线读数" prop="g1Reading">
        <el-input v-model="formData.g1Reading" placeholder="请输入1号伽玛射线读数" />
      </el-form-item>
      <el-form-item label="1号伽玛射线校正读数" prop="g1CReading">
        <el-input v-model="formData.g1CReading" placeholder="请输入1号伽玛射线校正读数" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { WITS8Api, WITS8VO } from '@/api/search/loggdata/wits8'

/** WITS8生成的表 表单 */
defineOptions({ name: 'WITS8Form' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  wellCode: undefined,
  deviceCode: undefined,
  recId: undefined,
  seqId: undefined,
  dataDate: undefined,
  dataTime: undefined,
  date: undefined,
  time: undefined,
  actCode: undefined,
  measDepth: undefined,
  vertDepth: undefined,
  bitDepth: undefined,
  bitVDepth: undefined,
  passNum: undefined,
  r1Depth: undefined,
  r1VDepth: undefined,
  r1Reading: undefined,
  r1CReading: undefined,
  r2Depth: undefined,
  r2VDepth: undefined,
  r2Reading: undefined,
  r2CReading: undefined,
  g1Depth: undefined,
  g1VDepth: undefined,
  g1Reading: undefined,
  g1CReading: undefined,
  remark: undefined,
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await WITS8Api.getWITS8(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as WITS8VO
    if (formType.value === 'create') {
      await WITS8Api.createWITS8(data)
      message.success(t('common.createSuccess'))
    } else {
      await WITS8Api.updateWITS8(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    wellCode: undefined,
    deviceCode: undefined,
    recId: undefined,
    seqId: undefined,
    dataDate: undefined,
    dataTime: undefined,
    date: undefined,
    time: undefined,
    actCode: undefined,
    measDepth: undefined,
    vertDepth: undefined,
    bitDepth: undefined,
    bitVDepth: undefined,
    passNum: undefined,
    r1Depth: undefined,
    r1VDepth: undefined,
    r1Reading: undefined,
    r1CReading: undefined,
    r2Depth: undefined,
    r2VDepth: undefined,
    r2Reading: undefined,
    r2CReading: undefined,
    g1Depth: undefined,
    g1VDepth: undefined,
    g1Reading: undefined,
    g1CReading: undefined,
    remark: undefined,
    state: undefined,
  }
  formRef.value?.resetFields()
}
</script>
